namespace DeviceCommons.Validation
{
    /// <summary>
    /// 设备消息验证异常
    /// 当输入参数不符合DeviceCommons框架要求时抛出
    /// </summary>
    public class DeviceMessageValidationException : ArgumentException
    {
        /// <summary>
        /// 验证错误类型
        /// </summary>
        public ValidationErrorType ErrorType { get; }

        /// <summary>
        /// 相关的参数名
        /// </summary>
        public string? ParameterName { get; }

        /// <summary>
        /// 预期值或范围描述
        /// </summary>
        public string? ExpectedValue { get; }

        /// <summary>
        /// 实际收到的值
        /// </summary>
        public object? ActualValue { get; }

        public DeviceMessageValidationException(
            ValidationErrorType errorType,
            string message,
            string? parameterName = null,
            string? expectedValue = null,
            object? actualValue = null)
            : base(message, parameterName)
        {
            ErrorType = errorType;
            ParameterName = parameterName;
            ExpectedValue = expectedValue;
            ActualValue = actualValue;
        }

        public DeviceMessageValidationException(
            ValidationErrorType errorType,
            string message,
            Exception innerException,
            string? parameterName = null)
            : base(message, parameterName, innerException)
        {
            ErrorType = errorType;
            ParameterName = parameterName;
        }

        public override string ToString()
        {
            var result = $"DeviceMessageValidationException: {Message}";
            result += $"\n  错误类型: {ErrorType}";
            
            if (!string.IsNullOrEmpty(ParameterName))
                result += $"\n  参数名: {ParameterName}";
            
            if (!string.IsNullOrEmpty(ExpectedValue))
                result += $"\n  期望值: {ExpectedValue}";
            
            if (ActualValue != null)
                result += $"\n  实际值: {ActualValue}";
            
            if (InnerException != null)
                result += $"\n  内部异常: {InnerException.Message}";
            
            return result;
        }
    }

    /// <summary>
    /// 验证错误类型枚举
    /// </summary>
    public enum ValidationErrorType
    {
        /// <summary>
        /// 设备ID无效
        /// </summary>
        InvalidDeviceId,

        /// <summary>
        /// 设备类型无效
        /// </summary>
        InvalidDeviceType,

        /// <summary>
        /// 状态ID无效
        /// </summary>
        InvalidStateId,

        /// <summary>
        /// 状态值无效
        /// </summary>
        InvalidStateValue,

        /// <summary>
        /// 状态值类型不匹配
        /// </summary>
        StateValueTypeMismatch,

        /// <summary>
        /// 消息数据长度不足
        /// </summary>
        InsufficientDataLength,

        /// <summary>
        /// 十六进制字符串格式无效
        /// </summary>
        InvalidHexFormat,

        /// <summary>
        /// 密码格式无效
        /// </summary>
        InvalidPassword,

        /// <summary>
        /// 设备数量超出限制
        /// </summary>
        DeviceCountExceeded,

        /// <summary>
        /// 读数数量超出限制
        /// </summary>
        ReadingCountExceeded,

        /// <summary>
        /// 状态数量超出限制
        /// </summary>
        StateCountExceeded,

        /// <summary>
        /// 时间偏移超出范围
        /// </summary>
        TimeOffsetOutOfRange,

        /// <summary>
        /// 主设备未配置
        /// </summary>
        MainDeviceRequired,

        /// <summary>
        /// 加密参数配置无效
        /// </summary>
        InvalidEncryptionConfiguration,

        /// <summary>
        /// 枚举值无效
        /// </summary>
        InvalidEnumValue,

        /// <summary>
        /// 内存使用超出限制
        /// </summary>
        MemoryLimitExceeded,

        /// <summary>
        /// 字符串长度超出限制
        /// </summary>
        StringLengthExceeded,

        /// <summary>
        /// 二进制数据长度超出限制
        /// </summary>
        BinaryDataLengthExceeded,

        /// <summary>
        /// 浮点数值无效（NaN或无穷大）
        /// </summary>
        InvalidFloatValue,

        /// <summary>
        /// 子设备添加前提条件不满足
        /// </summary>
        ChildDevicePrerequisiteNotMet,

        /// <summary>
        /// 缓冲区长度不足
        /// </summary>
        InsufficientBufferLength,

        /// <summary>
        /// 二进制数据格式无效
        /// </summary>
        InvalidBinaryDataFormat,

        /// <summary>
        /// 密文格式无效
        /// </summary>
        InvalidCipherTextFormat,

        /// <summary>
        /// Base64字符串格式无效
        /// </summary>
        InvalidBase64Format,

        /// <summary>
        /// 协议版本不支持
        /// </summary>
        UnsupportedProtocolVersion,

        /// <summary>
        /// 值类型转换失败
        /// </summary>
        ValueTypeConversionFailed,

        /// <summary>
        /// 工厂类型配置无效
        /// </summary>
        InvalidFactoryTypeConfiguration,

        /// <summary>
        /// 字节数组索引超出范围
        /// </summary>
        ByteArrayIndexOutOfRange,

        /// <summary>
        /// 状态数据不完整
        /// </summary>
        IncompleteStateData,

        /// <summary>
        /// 子设备数据长度无效
        /// </summary>
        InvalidChildDeviceDataLength,

        /// <summary>
        /// 输入参数无效
        /// </summary>
        InvalidInput,

        /// <summary>
        /// 配置无效
        /// </summary>
        InvalidConfiguration,

        /// <summary>
        /// 解析失败
        /// </summary>
        ParsingFailed,

        /// <summary>
        /// 序列化失败
        /// </summary>
        SerializationFailed
    }
}